package com.example.config.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志拦截器
 */
public class LogInterceptor implements HandlerInterceptor {

    /**
     * 进入 Controller 之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------ LoginInterceptor ---> preHandle  ------");

        return true;
    }

    /**
     * 调用完 Controller 之后，视图渲染之前
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("------ LoginInterceptor ---> postHandle  ------");
        System.out.println("请求路径：" + request.getRequestURI());
    }

    /**
     * 视图渲染之后
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("------ LoginInterceptor ---> afterCompletion  ------");
    }
}
